﻿using Dapper;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using System.Data;

namespace Todo.DAO.Dapper
{
    public class DbConnectionFactory : IDbConnectionFactory
    {
        private readonly string _connectionString;

        public DbConnectionFactory(IConfiguration configuration)
        {
            _connectionString = configuration.GetConnectionString("DefaultConnection")!;
            // 设置数据库方言（非常重要！）
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
        }

        public Task<IDbConnection> CreateConnectionAsync()
        {
            var connection = new MySqlConnection(_connectionString);
            connection.Open();
            return Task.FromResult<IDbConnection>(connection);
        }
    }
}
